package com.burst.reptile.modular.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.burst.reptile.common.constant.UserConstants;
import com.burst.reptile.common.exception.CustomException;
import com.burst.reptile.common.utils.ToolUtil;
import com.burst.reptile.modular.system.entity.SysPost;
import com.burst.reptile.modular.system.entity.SysUserPost;
import com.burst.reptile.modular.system.mapper.SysPostMapper;
import com.burst.reptile.modular.system.mapper.SysUserPostMapper;
import com.burst.reptile.modular.system.service.ISysPostService;

/**
 * <p>
 * 岗位信息表 服务实现类
 * </p>
 *
 * @author wujin
 * @since 2020-05-22
 */
@Service
public class SysPostServiceImpl extends ServiceImpl<SysPostMapper, SysPost> implements ISysPostService {
	
	@Autowired
	private SysUserPostMapper userPostMapper;

	@Override
	public List<SysPost> selectPostAll() {
		return this.list();
	}

	@Override
	public List<Integer> selectPostListByUserId(Long userId) {
		return this.baseMapper.selectPostListByUserId(userId);
	}

	@Override
	public Page<SysPost> selectPagePostList(Page<SysPost> page, SysPost post) {
		return this.baseMapper.selectPagePostList(page, post);
	}

	@Override
	public List<SysPost> selectPostList(SysPost post) {
		return this.baseMapper.selectPostList(post);
	}

	@Override
	public String checkPostNameUnique(SysPost post) {
		Long postId = ToolUtil.isEmpty(post.getPostId()) ? -1L : post.getPostId();
		SysPost info = this.baseMapper.selectOne(new QueryWrapper<SysPost>().eq("post_name", post.getPostName()));
		if (ToolUtil.isNotEmpty(info) && info.getPostId().longValue() != postId.longValue()) {
			return UserConstants.NOT_UNIQUE;
		}
		return UserConstants.UNIQUE;
	}

	@Override
	public String checkPostCodeUnique(SysPost post) {
		Long postId = ToolUtil.isEmpty(post.getPostId()) ? -1L : post.getPostId();
		SysPost info = this.baseMapper.selectOne(new QueryWrapper<SysPost>().eq("post_code", post.getPostCode()));
		if (ToolUtil.isNotEmpty(info) && info.getPostId().longValue() != postId.longValue()) {
			return UserConstants.NOT_UNIQUE;
		}
		return UserConstants.UNIQUE;
	}

	@Override
	public int insertPost(SysPost post) {
		return this.baseMapper.insert(post);
	}

	@Override
	public int updatePost(SysPost post) {
		return this.baseMapper.updateById(post);
	}

	@Override
	public int deletePostByIds(Long[] postIds) {
		for (Long postId : postIds) {
			SysPost post = this.getById(postId);
			if (countUserPostById(postId) > 0) {
				throw new CustomException(String.format("%1$s已分配,不能删除", post.getPostName()));
			}
		}
		return this.baseMapper.deletePostByIds(postIds);
	}

	@Override
	public int countUserPostById(Long postId) {
		
		return userPostMapper.selectCount(new QueryWrapper<SysUserPost>().eq("post_id", postId));
	}

	@Override
	public int updatePostStatus(SysPost post) {
		return this.baseMapper.updateById(post);
	}

}
